/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.dahg.project.controller.model;

import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 *
 * @author dahg
 */
@Entity
public class Factura {
    
    @Id
    @Basic
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    
    private String cliente;
    
    @Temporal(TemporalType.DATE)
    private Date fecha;
    
    private String mesa;
    
    private Boolean cerrada;
    
    @OneToMany(mappedBy = "factura",fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    private List<Item> items;
    
    
    @Transient
    public Double getTotal() {
        Double total=0d;
        
        if(items!=null && !items.isEmpty()) {
            for(Item item:items)
                total+=item.getCantidad()*item.getProducto().getPrecio();
        }
        
        return total;
    }
    
    public Factura() {
        
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCliente() {
        return cliente;
    }

    public void setCliente(String cliente) {
        this.cliente = cliente;
    }

    public Date getFecha() {
        return fecha;
    }

    public void setFecha(Date fecha) {
        this.fecha = fecha;
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    public String getMesa() {
        return mesa;
    }

    public void setMesa(String mesa) {
        this.mesa = mesa;
    }

    public Boolean getCerrada() {
        return cerrada;
    }

    public void setCerrada(Boolean cerrada) {
        this.cerrada = cerrada;
    }
    
    
}
